package top.hesisi.springboot3.service.message.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.hesisi.springboot3.aop.AuthInfo;
import top.hesisi.springboot3.base.PageBean;
import top.hesisi.springboot3.base.R;
import top.hesisi.springboot3.dao.message.TbMessageMapper;
import top.hesisi.springboot3.entity.message.TbMessage;
import top.hesisi.springboot3.interceptor.AppInterceptor;
import top.hesisi.springboot3.service.auth.AuthService;
import top.hesisi.springboot3.service.message.TbMessageService;
import top.hesisi.springboot3.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbMessageServiceImpl implements TbMessageService {
    private final TbMessageMapper tbMessageMapper;
    private final AuthService authService;

    @Override
    public R<String> insert(TbMessage tbMessage) {
        // 获取当前登录用户信息
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        // 发帖人
        tbMessage.setUid(authInfo.getUid());
        int i = tbMessageMapper.insert(tbMessage);
        return i == 1 ? R.ok("发布成功", "") : R.fail("发布失败", "");
    }

    @Override
    public R<PageVO<TbMessage>> query(TbMessage tbMessage, PageBean pageBean) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        QueryWrapper<TbMessage> wrapper = new QueryWrapper<>();
        // 定制查询的列
        wrapper.select("id", "uid", "title", "hits", "reply_count", "likes", "created");
        wrapper.like(StringUtils.hasText(tbMessage.getTitle()), "title", tbMessage.getTitle());
        wrapper.like(StringUtils.hasText(tbMessage.getContent()), "content", tbMessage.getContent());
        wrapper.orderByDesc("id");
        IPage<TbMessage> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        page = tbMessageMapper.selectPage(page, wrapper);

        // 获取发帖的人员信息
        page.getRecords().forEach(message -> {
            message.setUser(authService.queryById(message.getUid()).view());
            message.setMine(message.getUid().equals(authInfo.getUid()));
        });
        return R.ok(page);
    }

    @Override
    public R<String> delete(Integer id) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        // 校验帖子是否存在
        TbMessage tbMessage = tbMessageMapper.selectById(id);
        if (tbMessage == null) {
            return R.fail("帖子不存在", "");
        }
        // 只能删除用户自己的帖子
        if (!tbMessage.getUid().equals(authInfo.getUid())) {
            return R.fail("只能删除自己的帖子", "");
        }
        int i = tbMessageMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", "") : R.fail("删除失败", "");
    }

    @Override
    public R<String> update(TbMessage tbMessage) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        // 校验帖子是否存在
        TbMessage check = tbMessageMapper.selectById(tbMessage.getId());
        if (check == null) {
            return R.fail("帖子不存在", "");
        }
        // 帖子只能更新自己的
        if (!check.getUid().equals(authInfo.getUid())) {
            return R.fail("帖子只能更新自己的", "");
        }
        int i = tbMessageMapper.updateById(tbMessage);
        return i == 1 ? R.ok("更新成功", "") : R.fail("更新失败", "");
    }

    @Override
    public R<TbMessage> queryById(Integer id) {
        // 检查帖子是否存在
        TbMessage tbMessage = tbMessageMapper.selectById(id);
        if (tbMessage == null) {
            return R.fail("帖子不存在", null);
        }
        // 修改点击量
        tbMessageMapper.updateHits(id);
        tbMessage.setHits(tbMessage.getHits() + 1);
        // 获取发帖人员信息
        tbMessage.setUser(authService.queryById(tbMessage.getUid()).view());
        return R.ok("", tbMessage);
    }

}
